<template>
  <div class="carouse-item-container">
    <div class="scroll-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="轮播间隔" prop="duration" :rules="{ validator: valueRules, trigger: 'change' }">
          <el-input v-model="form.duration"><template slot="append">单位 / 秒</template></el-input>
        </el-form-item>
        <div v-for="(item, index) in form.list" :key="index" class="item">
          <div class="left">轮播{{ index + 1 }}：</div>
          <el-card>
            <el-form-item
              label="图片"
              :prop="'list.' + index + '.img'"
              :rules="{ required: true, message: '请上传图片' }"
            >
              <x-img-upload v-model="item.img" />
            </el-form-item>
            <el-form-item label="跳转路径" :prop="'list.' + index + '.toPath'">
              <el-input v-model="item.toPath" clearable />
            </el-form-item>
            <el-form-item
              label="排序"
              :prop="'list.' + index + '.sort'"
              :rules="{ validator: valueRules, trigger: 'change' }"
            >
              <el-input v-model="item.sort" clearable />
            </el-form-item>
          </el-card>
          <el-button type="primary" style="margin-left: 10px" @click="add">+轮播位</el-button>
          <el-button type="danger" @click="splice(index)">删除</el-button>
        </div>
      </el-form>
    </div>
    <div class="footer">
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </div>
</template>

<script>
import * as axiosApi from './api'
export default {
  data() {
    return {
      form: {
        duration: '3',
        list: [
          {
            img: '',
            toPath: '',
            sort: ' '
          }
        ]
      },
      rules: {}
    }
  },
  mounted() {
    // 回显
    axiosApi.getCarousel().then(res => {
      if(res.data.list.length){
        this.form = res.data
      }
    })
  },
  methods: {
    valueRules: (rule, value, callback) => {
      if (isNaN(value)) {
        return callback(new Error('请输入数字'))
      }
      return callback()
    },
    add() {
      this.form.list.push({ img: '', toPath: '', sort: '' })
    },
    splice(index) {
      this.form.list.splice(index, 1)
      axiosApi.updateCarousel(this.form).then(() => {
        this.$message({
          message: '删除成功'
        })
      })
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          axiosApi.updateCarousel(this.form).then(() => {
            this.$message({
              message: '保存成功'
            })
          })
        }
      })
    },
    reset() {
      this.form.list = [
        {
          img: '',
          toPath: '',
          sort: ' '
        }
      ]
    }
  }
}
</script>

<style lang="scss" scoped>
.carouse-item-container {
  height: 100%;
  position: relative;
  .scroll-container {
    height: 100%;
    padding-bottom: 70px;
    padding-top: 10px;
    overflow-y: auto;
  }
  .item {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    margin-top: 10px;
  }
  .footer {
    box-shadow: 0 -2px 12px 0 rgb(0 0 0 / 10%);
    position: absolute;
    display: flex;
    justify-content: center;
    background-color: #fff;
    width: 100%;
    bottom: 0px;
    padding: 10px 0;
  }
}
</style>
